
def fibo(num):
    a, b = 0, 1
    cur_num = 0
    while cur_num < num:
        # print(a) 
        yield a  # 如果一个def中有yield函数 那么这个def就是生成器模板
        a, b = b, a + b
        cur_num += 1
    return 'ok'

# 如果调用def的里面有yield，那么调用的时候就是创建一个生成器对象
f = fibo(10)

# 取值
while True:
    try:
        res = next(f)
        print(res)
    except Exception as res:
        print(res.value)  # 获取生成器的返回值
        break
